<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:p="http://primefaces.org/ui">

<ui:composition template="/auth/templates/template.xhtml">
	<ui:define name="pageTitle">Novo Pedido de Venda</ui:define>
	<ui:define name="body">
		<f:view>

			<f:metadata>
				<f:viewParam name="pedidoVenda" value="#{vendaVSBean.pedidoVenda}" />
			</f:metadata>

			<p:growl id="messages" autoUpdate="true" closable="true"
				showDetail="true" />

			<h:form id="form">
				<h:panelGrid cellspacing="10px" styleClass="center">

					<f:facet name="header">Pedido de Venda a Vista</f:facet>

					<p:commandLink value="Abrir Novo Pedido Venda"
						action="#{vendaVSBean.abrirPedido}" update="@form"
						rendered="#{not vendaVSBean.exibirPedidoVenda}" />

					<p:focus context="form" />

					<h:panelGrid cellspacing="10px"
						rendered="#{vendaVSBean.exibirPedidoVenda}">
						<h:panelGrid columns="3" cellspacing="10px">
							<h:outputLabel value="Local:" />
							<h:inputText value="#{vendaVSBean.local}" />
							<p:commandButton
								actionListener="#{vendaVSBean.selecionarProdutoEmEstoque}"
								icon="ui-icon-search" title="Procurar" update="@form" />
						</h:panelGrid>
						<p:dataTable id="tabelaItensPedidoVenda"
							value="#{vendaVSBean.pedidoVenda.itensPedidoVenda}"
							var="itemPedidoVenda" style="text-align:center;"
							emptyMessage="Não existem registros para esta tabela">

							<f:facet name="header">
								<h:outputText
									value="Pedido de Venda  ##{vendaVSBean.pedidoVenda.idPedidoVenda}" />
							</f:facet>

							<p:column headerText="Produto">
								<h:outputText
									value="#{itemPedidoVenda.itemEstoque.produto.categoria.nome} #{itemPedidoVenda.itemEstoque.produto.descricao}" />
							</p:column>
							<p:column headerText="Preço">
								<h:outputText value="#{itemPedidoVenda.itemEstoque.preco}">
									<f:convertNumber currencyCode="BRL" type="currency"
										minFractionDigits="3" />
								</h:outputText>
							</p:column>

							<p:column headerText="Quantidade">
								<f:ajax event="change"
									render=":form:tabelaItensPedidoVenda :form:txtvalortotal">

									<p:spinner value="#{itemPedidoVenda.quantidade}"
										rendered="#{not vendaVSBean.exibirControle('G', itemPedidoVenda) and not vendaVSBean.pedidoVenda.status}"
										min="0" size="2" required="true"
										title="#{itemPedidoVenda.itemEstoque.local}"
										validator="quantidadeItemPedidoVendaValidator" />
									<p:spinner value="#{itemPedidoVenda.quantidade}"
										rendered="#{vendaVSBean.exibirControle('G', itemPedidoVenda) and not vendaVSBean.pedidoVenda.status}"
										stepFactor="5" min="0" size="2" required="true"
										title="#{itemPedidoVenda.itemEstoque.local}"
										validator="quantidadeItemPedidoVendaValidator" />

									<h:outputText value="#{itemPedidoVenda.quantidade}"
										rendered="#{vendaVSBean.pedidoVenda.status}" />
								</f:ajax>
								<h:outputText
									value=" (#{itemPedidoVenda.itemEstoque.produto.unidade.label})" />
							</p:column>

							<p:column headerText="SubTotal">
								<h:outputText value="#{itemPedidoVenda.subTotal}">
									<f:convertNumber currencyCode="BRL" type="currency"
										minFractionDigits="3" />
								</h:outputText>
							</p:column>
							<p:column headerText="Ações"
								rendered="#{not vendaVSBean.pedidoVenda.status}">
								<p:commandButton update=":form :form:tabelaItensPedidoVenda"
									icon="ui-icon-closethick" title="Remover"
									action="#{vendaVSBean.removerItemPedidoVenda()}"
									immediate="true">
									<f:setPropertyActionListener value="#{itemPedidoVenda}"
										target="#{vendaVSBean.itemPedidoVendaSelecionado}" />
								</p:commandButton>
							</p:column>
						</p:dataTable>
						<h:panelGrid cellspacing="10px" columns="2"
							styleClass="panel-total-venda">
							<h:outputText value="Total" />
							<h:outputText id="txtvalortotal"
								value="#{vendaVSBean.pedidoVenda.valor}">
								<f:convertNumber currencyCode="BRL" type="currency" />
							</h:outputText>
						</h:panelGrid>
						<p:commandButton
							actionListener="#{vendaVSBean.concluirPedidoVenda}"
							value="Concluir Pedido" update="@form :form:trocoDlg"
							oncomplete="PF('trocoDialog').show(); return false; "
							icon="ui-icon-circle-arrow-e"
							rendered="#{not vendaVSBean.pedidoVenda.status}" />
					</h:panelGrid>
				</h:panelGrid>


				<p:dialog header="Calcular Troco" widgetVar="trocoDialog"
					resizable="false" id="trocoDlg" showEffect="fade"
					rendered="#{vendaVSBean.pedidoVenda.status}">

					<p:panel id="trocoPanel" style="border:nome;">
						<h:panelGrid columns="2" cellspacing="10px" style="font-size:2em;">

							<f:ajax event="keyup" render="troco">

								<h:outputLabel value="Valor Total" for="valorTotal" />
								<h:outputText id="valorTotal"
									value="#{vendaVSBean.pedidoVenda.valor}">
									<f:convertNumber currencyCode="BRL" type="currency"
										minFractionDigits="3" />
								</h:outputText>

								<h:outputLabel value="Recebido" for="recebido" />
								<h:inputText id="recebido" value="#{vendaVSBean.recebido}"
									onfocus="this.value=''" autocomplete="off">
									<f:convertNumber minFractionDigits="3" />
								</h:inputText>

								<h:outputLabel value="Desconto" for="desconto" />
								<h:inputText id="desconto" value="#{vendaVSBean.venda.desconto}"
									onfocus="this.value=''" autocomplete="off">
									<f:convertNumber minFractionDigits="3" />
								</h:inputText>
								<h:outputLabel value="Troco" for="troco"
									rendered="#{not empty vendaVSBean.troco}" />
								<h:outputText id="troco" value="#{vendaVSBean.troco}"
									rendered="#{not empty vendaVSBean.troco}">
									<f:convertNumber currencyCode="BRL" type="currency"
										minFractionDigits="3" />
								</h:outputText>

							</f:ajax>

						</h:panelGrid>

						<p:commandButton value="Enviar" update="@form"
							onclick="PF('itemPedidoVendaDialog').hide(); return false; "
							action="#{vendaVSBean.fecharVendaAVista()}" />

					</p:panel>
				</p:dialog>
			</h:form>
		</f:view>
	</ui:define>
</ui:composition>

</html>